package com.qq.first_project.controller.common;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qq.first_project.common.R;
import com.qq.first_project.entity.Record;
import com.qq.first_project.entity.vo.RecordVo;
import com.qq.first_project.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Qiu
 * @since 2023-02-10
 */
@RestController
@CrossOrigin //跨域
@RequestMapping("/common/record")
public class RecordController {

    @Autowired
    private RecordService recordService;

    /**
     * 管理系统查询
     * @return
     */
    @GetMapping("list")
    R list(){
        List<RecordVo> list = recordService.recordList();
        return R.ok().data("data",list).message("获取成功");
    }

    /**
     * 博客页面查询
     * @return
     */
    @GetMapping("page-list")
    R pageList(ModelMap modelMap,@RequestParam("current") int num,@RequestParam("pageSize") int size,String name){
        Page<RecordVo> page = new Page<>(num,size);
        IPage<RecordVo> list = recordService.pageList(page,name);
        modelMap.addAttribute("page",list);
        return R.ok().data("data",list).message("获取成功");
    }

    /**
     * 新增
     * @param record
     * @return
     */
    @PostMapping("save")
    R save(Record record){
        boolean result = recordService.save(record);
        if (result) {
            return R.ok().message("保存成功");
        } else {
            return R.error().message("保存失败");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("delById")
    R delById(Long id){
        boolean result = recordService.removeById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }

    /**
     * 修改
     * @param record
     * @return
     */
    @PostMapping("updateById")
    R updateById(Record record){
        boolean result = recordService.updateById(record);
        if (result) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("数据不存在");
        }
    }

}
